// XmlDataElement.cpp: implementation of the XmlDataElement class.
//
//////////////////////////////////////////////////////////////////////

#include "XmlDataElement.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

XmlDataElement::XmlDataElement(File f)
{
	if (f.exists())
	{
		file = f;
	}
	else
	{
		file = File::nonexistent;
	}
}

XmlDataElement::XmlDataElement(MemoryBlock mb)
{
	if (mb.getSize() > 0)
	{
		memoryBlock = mb;
	}
	else
	{
		memoryBlock = 0;
	}
}

XmlDataElement::XmlDataElement(const uint8 *data, unsigned int size)
{
	file = File::nonexistent;
	memoryBlock.append  (data, size);
}

XmlDataElement::~XmlDataElement()
{
	file = File::nonexistent;
	memoryBlock.setSize(0);
}

XmlElement XmlDataElement::getXmlElement(String elementName)
{
	XmlElement xmlElement(elementName);
	String base64Data;

	if (file.exists())
	{
		if (file.loadFileAsData (memoryBlock))
		{
			base64Data = memoryBlock.toBase64Encoding();
		}
		else
		{
			return (xmlElement);
		}
	}
	else if (memoryBlock.getSize() > 0)
	{
		base64Data = memoryBlock.toBase64Encoding();
	}

	xmlElement.setAttribute (T("data-base64-size"), memoryBlock.getSize());
	xmlElement.setAttribute (T("data-base64"), base64Data);

	return (xmlElement);
}